using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Soyuz5.Utility
{
    public static class Data
    {
        public static DataRow CloneRow(DataRow Row)
        {
            DataRow new_row = Row.Table.NewRow();
            CopyRowData(Row, new_row);
            return new_row;
        }

        public static void DeleteAllRows(DataSet Data)
        {
            foreach (DataTable table in Data.Tables)
            {
                DeleteAllRows(table);
            }
        }

        public static void DeleteAllRows(DataTable Table)
        {
            foreach (DataRow row in Table.Select())
            {
                row.Delete();
            }
        }

        /// <summary>
        /// Copy row data to another row,
        /// </summary>
        /// <param name="RowFrom"></param>
        /// <param name="RowTo"></param>
        public static void CopyRowData(DataRow RowFrom, DataRow RowTo)
        {
            object[] item_array = (object[])RowFrom.ItemArray.Clone();
            for (int i = 0; i < RowTo.Table.Columns.Count; i++)
            {
                foreach (DataColumn dc in RowTo.Table.PrimaryKey)
                {
                    if (dc == RowTo.Table.Columns[i])
                    {
                        item_array[i] = null;
                    }
                }
            }
            RowTo.ItemArray = item_array;
        }
    }
}
